package com.wsjzzcbq.qianyuan.sys.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsjzzcbq.qianyuan.common.bean.PageBean;
import com.wsjzzcbq.qianyuan.common.bean.R;
import com.wsjzzcbq.qianyuan.common.cache.CacheService;
import com.wsjzzcbq.qianyuan.common.constant.CacheConsts;
import com.wsjzzcbq.qianyuan.common.constant.MsgConsts;
import com.wsjzzcbq.qianyuan.common.constant.SysConsts;
import com.wsjzzcbq.qianyuan.common.util.CookieUtils;
import com.wsjzzcbq.qianyuan.common.util.PasswordUtils;
import com.wsjzzcbq.qianyuan.common.util.SysUtils;
import com.wsjzzcbq.qianyuan.sys.bean.SysUser;
import com.wsjzzcbq.qianyuan.sys.mapper.SysUserMapper;
import com.wsjzzcbq.qianyuan.sys.param.AddSysUserParam;
import com.wsjzzcbq.qianyuan.sys.param.FindSysUserListParam;
import com.wsjzzcbq.qianyuan.sys.param.LoginParam;
import com.wsjzzcbq.qianyuan.sys.service.SysUserService;
import com.wsjzzcbq.qianyuan.sys.vo.LoginVo;
import com.wsjzzcbq.qianyuan.sys.vo.SysUserInfoVo;
import com.wsjzzcbq.qianyuan.sys.vo.SysUserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * SysUserServiceImpl
 *
 * @author wsjz
 * @date 2022/03/10
 */
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public R<LoginVo> login(LoginParam param) {
        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysUser::getUserName, param.getUsername());
        SysUser sysUser = this.getOne(lqw);
        if (sysUser == null) {
            return R.error(MsgConsts.USER_NAME_INCORRECT);
        }
        String userPwd = PasswordUtils.encrypt(param.getPassword(), param.getUsername());
        if (!userPwd.equals(sysUser.getPassword())) {
            return R.error(MsgConsts.PASSWORD_INCORRECT);
        }

        String token = SysUtils.token();
        LoginVo vo = new LoginVo();
        vo.setToken(token);

        cacheService.set(CacheConsts.userKey(token), JSONObject.toJSONString(sysUser));

        return R.success(vo);
    }

    @Override
    public R<SysUserInfoVo> getSysUserInfo(String token) {
        String value = cacheService.get(CacheConsts.userKey(token));
        if (StringUtils.isBlank(value)) {
            return R.error(MsgConsts.TOKEN_INCORRECT);
        }
        SysUser sysUser = JSONObject.parseObject(value, SysUser.class);
        if (sysUser != null) {
            SysUserInfoVo vo = new SysUserInfoVo();
            //姓名
            vo.setName(sysUser.getName());
            //头像
            vo.setAvatar(sysUser.getHeadImg());

            vo.setToken(token);

            return R.success(vo);
        }
        return R.error(MsgConsts.TOKEN_INCORRECT);
    }

    @Override
    public R<?> logout() {
        String token = CookieUtils.getValyeByName(SysConsts.COOKIE_TOKEN);
        cacheService.remove(CacheConsts.userKey(token));
        cacheService.remove(CacheConsts.permissionKey(token));
        return R.success();
    }

    @Override
    public R<PageBean<SysUserVo>> findList(FindSysUserListParam param) {
        QueryWrapper<SysUser> lqw = new QueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            lqw.like("name", param.getName());
        }
        if (param.getCreateDate() != null && !param.getCreateDate().isEmpty()) {
            lqw.between("DATE_FORMAT(create_date,'%Y-%m-%d')", param.getCreateDate().get(0), param.getCreateDate().get(1));
        }
        lqw.orderByDesc("create_date");

        IPage<SysUserVo> iPage = sysUserMapper.findPageList(new Page<>(param.getPageNum(), param.getPageSize()), param);
        PageBean<SysUserVo> pageBean = PageBean.create(iPage);

        return R.success(pageBean);
    }

    @Override
    public R<?> addOrUpdate(AddSysUserParam param) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(param, sysUser);
        sysUser.setRoleId(Long.valueOf(param.getRoleId()));

        if (StringUtils.isBlank(param.getId())) {
            //添加
            this.save(sysUser);
            return R.success(MsgConsts.ADD_SUCCESS);
        } else {
            //修改
            long id = Long.valueOf(param.getId());
            SysUser user = this.getById(id);
            if (user == null) {
                return R.error(MsgConsts.ID_ERROR);
            }
            sysUser.setId(id);
            this.updateById(sysUser);
            return R.success(MsgConsts.UPDATE_SUCCESS);
        }
    }

    @Override
    public R<?> findSysUserById(String id) {
        if (StringUtils.isBlank(id)) {
            return R.error(MsgConsts.ID_NOT_NULL);
        }
        SysUser sysUser = this.getById(id);
        if (sysUser == null) {
            return R.error(MsgConsts.ID_ERROR);
        }
        SysUserVo vo = new SysUserVo();
        BeanUtils.copyProperties(sysUser, vo);
        vo.setId(String.valueOf(sysUser.getId()));
        vo.setRoleId(String.valueOf(sysUser.getRoleId()));
        return R.success(vo);
    }

    @Override
    public R<?> deleteSysUserById(String id) {
        if (StringUtils.isBlank(id)) {
            return R.error(MsgConsts.ID_NOT_NULL);
        }
        SysUser sysUser = this.getById(id);
        if (sysUser == null) {
            return R.error(MsgConsts.ID_ERROR);
        }
        this.removeById(id);
        return R.success(MsgConsts.DELETE_SUCCESS);
    }
}
